<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是STM32开发——使用MDK开发的时候SCT文件的应用相关的一些相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV16-06-MDK-03-SCT文件应用">
<meta property="og:url" content="https://sumumm.github.io/post/4329ee27.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是STM32开发——使用MDK开发的时候SCT文件的应用相关的一些相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230529215228408.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/MDK01.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530075632958.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530080034105.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530080717236.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530083036138.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530084036075.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/MDK006.jpeg">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530093755738.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530094653845.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530095728076.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530095921199.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530101824606.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530102348151.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530103740629.png">
<meta property="article:published_time" content="2023-06-04T14:06:33.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.007Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV16-STM32开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230529215228408.png">


<link rel="canonical" href="https://sumumm.github.io/post/4329ee27.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/4329ee27.html","path":"post/4329ee27.html","title":"LV16-06-MDK-03-SCT文件应用"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV16-06-MDK-03-SCT文件应用 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89SCT%E6%96%87%E4%BB%B6"><span class="nav-text">一、自定义SCT文件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%B0%86%E5%86%85%E9%83%A8SRAM%E4%B8%80%E5%88%86%E4%B8%BA%E4%BA%8C"><span class="nav-text">二、将内部SRAM一分为二</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E9%BB%98%E8%AE%A4%E9%80%89%E6%8B%A9%E6%9C%80%E5%A4%A7%E5%86%85%E5%AD%98%E7%9A%84%E5%8C%BA%E5%9F%9F%E6%B5%8B%E8%AF%95"><span class="nav-text">1. 默认选择最大内存的区域测试</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81"><span class="nav-text">1.1 测试代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-sct%E6%96%87%E4%BB%B6%E4%BF%AE%E6%94%B9"><span class="nav-text">1.2 sct文件修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E7%BB%93%E6%9E%9C%E5%88%86%E6%9E%90"><span class="nav-text">1.3 结果分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-map%E6%96%87%E4%BB%B6%E5%88%86%E6%9E%90"><span class="nav-text">1.4 map文件分析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AE%9A%E4%B9%89%E6%A0%88%E5%8C%BA%E5%88%B0%E6%8C%87%E5%AE%9A%E5%86%85%E5%AD%98%E7%9A%84%E5%8C%BA%E5%9F%9F"><span class="nav-text">2. 定义栈区到指定内存的区域</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81"><span class="nav-text">2.1 测试代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-sct%E6%96%87%E4%BB%B6%E4%BF%AE%E6%94%B9"><span class="nav-text">2.2 sct文件修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E7%BB%93%E6%9E%9C%E5%88%86%E6%9E%90"><span class="nav-text">2.3 结果分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-map%E6%96%87%E4%BB%B6%E5%88%86%E6%9E%90"><span class="nav-text">2.4 map文件分析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AE%9A%E4%B9%89%E5%A0%86%E5%8C%BA%E5%88%B0%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AD%98%E5%8C%BA%E5%9F%9F"><span class="nav-text">3. 定义堆区到指定的内存区域</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81"><span class="nav-text">3.1 测试代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-sct%E6%96%87%E4%BB%B6%E4%BF%AE%E6%94%B9"><span class="nav-text">3.2 sct文件修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E7%BB%93%E6%9E%9C%E5%88%86%E6%9E%90"><span class="nav-text">3.3 结果分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-map%E6%96%87%E4%BB%B6%E5%88%86%E6%9E%90"><span class="nav-text">3.4 map文件分析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E8%8A%82%E5%8C%BA"><span class="nav-text">4. 自定义一个节区</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81"><span class="nav-text">4.1 测试代码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-sct%E6%96%87%E4%BB%B6%E4%BF%AE%E6%94%B9"><span class="nav-text">4.2 sct文件修改</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-1-%E9%BB%98%E8%AE%A4%E6%83%85%E5%86%B5"><span class="nav-text">4.2.1 默认情况</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-2-%E6%8C%87%E5%AE%9A%E5%88%B0%E8%BE%83%E5%B0%8F%E7%9A%84%E6%89%A7%E8%A1%8C%E5%9F%9F"><span class="nav-text">4.2.2 指定到较小的执行域</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E9%80%89%E6%8B%A9%E6%80%A7%E4%BD%BF%E7%94%A8%E5%A4%96%E9%83%A8SRAM"><span class="nav-text">三、选择性使用外部SRAM</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-SRAM%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">1. SRAM初始化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E6%96%B0%E7%9A%84%E6%89%A7%E8%A1%8C%E5%9F%9F"><span class="nav-text">2. 定义一个新的执行域</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E4%B8%8D%E6%8C%87%E5%AE%9A%E6%A0%88%E5%8C%BA"><span class="nav-text">2.1 不指定栈区</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E6%8C%87%E5%AE%9A%E6%A0%88%E5%8C%BA"><span class="nav-text">2.2 指定栈区</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E5%8F%AA%E6%8C%87%E5%AE%9A%E7%89%B9%E5%AE%9A%E7%9A%84%E5%8F%98%E9%87%8F%E5%88%B0%E5%A4%96%E9%83%A8SRAM"><span class="nav-text">2.3 只指定特定的变量到外部SRAM</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-%E5%A0%86%E7%A9%BA%E9%97%B4"><span class="nav-text">2.4 堆空间</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81"><span class="nav-text">3. 测试代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E7%BB%93%E6%9E%9C%E5%88%86%E6%9E%90"><span class="nav-text">4. 结果分析</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/4329ee27.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV16-06-MDK-03-SCT文件应用 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV16-06-MDK-03-SCT文件应用
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-06-04 22:06:33" itemprop="dateCreated datePublished" datetime="2023-06-04T22:06:33+08:00">2023-06-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV16-STM32开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>8.2k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>30 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是STM32开发——使用MDK开发的时候SCT文件的应用相关的一些相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <ul><li>通用</li></ul><table><tr><td align="center">分类  </td><td align="center">网址</td><td align="center">说明</td></tr><tr><td align="center" rowspan="4">官方网站</td><td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td><td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td></tr><tr>                                            <td align="left"><a href="https://www.st.com/content/st_com/zh.html" target="_blank">https://www.st.com/content/st_com/zh.html</a></td><td align="left">ST官方网站，在这里我们可以找到STM32的相关文档</td></tr><tr>                                            <td align="left"><a href="https://www.stmcu.com.cn/" target="_blank">https://www.stmcu.com.cn/</a></td><td align="left">意法半导体ST中文官方网站，在这里我们可以找到STM32的相关中文参考文档</td></tr><tr>                                            <td align="left"><a href="http://elm-chan.org/fsw/ff/00index_e.html" target="_blank">http://elm-chan.org/fsw/ff/00index_e.html</a></td><td align="left">FatFs文件系统官网</td></tr><tr><td align="center" rowspan="3">教程书籍</td><td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M3权威指南(中文).pdf" target="_blank">《ARM Cortex-M3权威指南》</a></td><td alirn="left" rowspan="3">ARM公司专家Joseph Yiu（姚文祥）的力作，中文翻译是NXP的宋岩</td></tr><tr>                                            <td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M0权威指南(中文).pdf" target="_blank">《ARM Cortex-M0权威指南》</a></td></tr><tr>                                            <td align="left"><a href="" target="_blank">《ARM Cortex-M3与Cortex-M4权威指南》</a></td></tr><tr><td align="center" rowspan="4">开发论坛</td><td align="left"><a href="http://47.111.11.73/forum.php" target="_blank">http://47.111.11.73/forum.php</a></td><td align="left">开源电子网，正点原子的资料下载及问题讨论论坛</td></tr><tr>                                            <td align="left"><a href="https://www.firebbs.cn/forum.php" target="_blank">https://www.firebbs.cn/forum.php</a></td><td align="left">国内Kinetis开发板-野火/秉火（刘火良）主持的论坛，现也做STM32和i.MX RT</td></tr><tr>                                            <td align="left"><a href="https://www.amobbs.com/index.php" target="_blank">https://www.amobbs.com/index.php</a></td><td align="left">阿莫（莫进明）主持的论坛，号称国内最早最火的电子论坛，以交流Atmel AVR系列单片机起家，现已拓展到嵌入式全平台，其STM32系列帖子有70W+。</td></tr><tr>                                            <td align="left"><a href="http://download.100ask.net/index.html" target="_blank">http://download.100ask.net/index.html</a></td><td align="left">韦东山嵌入式资料中心，有些STM32和linux的相关资料也可以来这里找。</td></tr><tr><td align="center" rowspan="3">博客参考</td><td align="left"><a href="http://www.openedv.com/" target="_blank">http://www.openedv.com/</a></td><td align="left">开源网-原子哥个人博客</td></tr><tr>                                            <td align="left"><a href="http://blog.chinaaet.com/jihceng0622" target="_blank">http://blog.chinaaet.com/jihceng0622</a></td><td align="left">博主是原Freescale现NXP的现场应用工程师</td></tr><tr>                                            <td align="left"><a href="https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/cortex-m-resources" target="_blank">cortex-m-resources</a></td><td align="left">这其实并不算是一个博客，这是ARM公司专家Joseph Yiu收集整理的所有对开发者有用的官方Cortex-M资料链接（也包含极少数外部资源链接）</td></tr></table><ul><li>STM32</li></ul><table>    <tr><td align="center" rowspan="2">STM32</td><td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103/hal_general/zh/latest/index.html" target="_blank">STM32 HAL库开发实战指南——基于F103系列开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr><tr>                                            <td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103badao/std/zh/latest/index.html" target="_blank">STM32库开发实战指南——基于野火霸道开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr></table><ul><li>SD卡</li></ul><table>    <tr><td align="left"><a href="https://www.sdcard.org/" target="_blank">SD Association</a></td><td align="left">提供了SD存储卡和SDIO卡系统规范</td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr><td align="left"><a href="https://www.st.com/resource/en/datasheet/stm32f103ze.pdf" target="_blank">STM32F103xx英文数据手册</a></td><td align="left">STM32F103xC/D/E系列的英文数据手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/709978" target="_blank">STM32F103xx中文数据手册</a></td><td align="left">STM32F103xC/D/E系列的中文数据手册</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf" target="_blank">STM32F10xxx英文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的英文参考手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/710001" target="_blank">STM32F10xxx中文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的中文参考手册</td></tr>    <tr><td align="left"><a href="https://developer.arm.com/documentation/100165/0201/?lang=en" target="_blank">Arm Cortex-M3 处理器技术参考手册-英文版</a></td><td align="left">Cortex-M3技术参考手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf" target="_blank">STM32F10xxx Cortex-M3编程手册-英文版(PM0056)</a></td><td align="left">STM32F10xxx/20xxx/21xxx/L1xxxx系列Cortex-M3编程手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/" target="_blank">SD卡相关资料——最新版本</a></td><td align="left">有关SD卡的一些资料可以从这里下载</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/archives/" target="_blank">SD卡相关资料——历史版本</a></td><td align="left">有关SD卡的一些历史版本资料可以从这里下载，比如后边看的SD卡2.0协议</td></tr>    <tr><td align="left"><a href="./" target="_blank">SD 2.0 协议标准完整版</a></td><td align="left">这是一篇关于SD卡2.0协议的中文文档，还是比较有参考价值的，可以一看</td></tr></table>
              </div>
            </details> 

<p>这一篇笔记主要是通过SCT文件来实现变量的分配位置，这里需要后边FSMC驱动SRAM的知识。由于内存管理对应用程序非常重要，若修改 sct 文件，不使用默认配置，对工程影响非常大，容易导致出错，所以我们这里也是跟着教程做了两个实验，来学习 sct 文件的应用细节。</p>
<h1 id="一、自定义SCT文件"><a href="#一、自定义SCT文件" class="headerlink" title="一、自定义SCT文件"></a><font size=3>一、自定义SCT文件</font></h1><p>注意这里我们要根据上一节笔记，取消SDK使用默认的SCT文件。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230529215228408.png" alt="image-20230529215228408" style="zoom: 33%;" />

<p>在 MDK 的“【Options for Target】&rarr;【Linker】&rarr;【Use Memory Layout from Target Dialog】”选项被取消勾选，取消勾选后可直接点击“Edit”按钮编辑工程的 sct 文件，也可到工程目录下打开编辑 。我们先回顾一下程序中各部分的类别：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/MDK01.png" alt="程序组件所属的区域" style="zoom:50%;" />

<h1 id="二、将内部SRAM一分为二"><a href="#二、将内部SRAM一分为二" class="headerlink" title="二、将内部SRAM一分为二"></a><font size=3>二、将内部SRAM一分为二</font></h1><p>我们先来做这样一个实验，就是将内部的64KB SRAM分为两部分，然后来将定义的变量分配到指定的地址区间。</p>
<h2 id="1-默认选择最大内存的区域测试"><a href="#1-默认选择最大内存的区域测试" class="headerlink" title="1. 默认选择最大内存的区域测试"></a><font size=3>1. 默认选择最大内存的区域测试</font></h2><h3 id="1-1-测试代码"><a href="#1-1-测试代码" class="headerlink" title="1.1 测试代码"></a><font size=3>1.1 测试代码</font></h3><p>我们定义一个全局变量和数组，还有一个局部变量，还有一个堆区的变量（通过malloc分配），如下所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">uint32_t</span> testValue = <span class="number">129</span>;</span><br><span class="line"><span class="type">uint8_t</span> testGroup[<span class="number">20</span>] = &#123;<span class="number">45</span>, <span class="number">65</span>, <span class="number">78</span>, <span class="number">54</span>, <span class="number">121</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    HAL_Init();</span><br><span class="line">    SystemClock_Config();</span><br><span class="line">    MX_GPIO_Init();</span><br><span class="line">    MX_USART1_UART_Init();</span><br><span class="line">    MX_FSMC_Init();</span><br><span class="line"></span><br><span class="line">    testValue ++;</span><br><span class="line">    testGroup[<span class="number">0</span>]++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testValue:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;testValue, testValue);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testGroup:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)testGroup, testGroup[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">    <span class="type">uint8_t</span> inerTestValue = <span class="number">19</span>;</span><br><span class="line">    inerTestValue++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;inerTestValue:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;inerTestValue, inerTestValue);</span><br><span class="line">	</span><br><span class="line">	<span class="type">uint8_t</span> *p;</span><br><span class="line">	p = (<span class="type">uint8_t</span> *)<span class="built_in">malloc</span>(<span class="number">20</span>);</span><br><span class="line">	*p = <span class="number">50</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;p:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)p, *p);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        LED0 = !LED0;</span><br><span class="line">        LED1 = !LED1;</span><br><span class="line">        HAL_Delay(<span class="number">500</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>完整工程可以查看<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/stm32f103-prj">STM32F103-Prj: STM32学习使用（STM32CubeMX+Makefile+VScode+J-Flash） (gitee.com)</a>。</p>
<h3 id="1-2-sct文件修改"><a href="#1-2-sct文件修改" class="headerlink" title="1.2 sct文件修改"></a><font size=3>1.2 sct文件修改</font></h3><p>我们将sct文件修改成如下内容：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">; *************************************************************</span><br><span class="line">; *** Scatter-Loading Description File generated by uVision ***</span><br><span class="line">; *************************************************************</span><br><span class="line"></span><br><span class="line">LR_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;    ; load region size_region</span><br><span class="line">  ER_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;  ; load address = execution address</span><br><span class="line">   *.o (RESET, +First)</span><br><span class="line">   *(InRoot$$Sections)</span><br><span class="line">   .ANY (+RO)</span><br><span class="line">   .ANY (+XO)</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-3-结果分析"><a href="#1-3-结果分析" class="headerlink" title="1.3 结果分析"></a><font size=3>1.3 结果分析</font></h3><p>我们的sct文件中执行域是这样的：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line"> .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">&#125;</span><br><span class="line">RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line"> .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里没有规定栈区，堆区以及一些特定的变量要分配在哪，而且第二块区域明显比第一块区域大，所以我们分配变量的时候优先在第二块区域，并且栈区和堆区也将会在第二块区域，我们来看一下打印信息：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">testValue:<span class="number">0x20005000</span> <span class="number">130</span></span><br><span class="line">testGroup:<span class="number">0x20005004</span> <span class="number">46</span></span><br><span class="line">inerTestValue:<span class="number">0x20005768</span> <span class="number">20</span></span><br><span class="line">p:<span class="number">0x20005188</span> <span class="number">50</span></span><br></pre></td></tr></table></figure>

<p>可以看到全局变量以及全局变量数组，包括栈区的局部变量还有malloc申请的堆区都被分配在了0x20005000之后的区域了。</p>
<h3 id="1-4-map文件分析"><a href="#1-4-map文件分析" class="headerlink" title="1.4 map文件分析"></a><font size=3>1.4 map文件分析</font></h3><p>我们再来看看map文件的Memory Map of the image部分，就会发现这里多了一个执行域啦。而且注意RW_IRAM1中没有分配任何东西。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530075632958.png" alt="image-20230530075632958" style="zoom:50%;" />

<p>我们可以看到，栈区和堆区也都被自动分配在了较大的RW_IRAM2中。</p>
<h2 id="2-定义栈区到指定内存的区域"><a href="#2-定义栈区到指定内存的区域" class="headerlink" title="2. 定义栈区到指定内存的区域"></a><font size=3>2. 定义栈区到指定内存的区域</font></h2><h3 id="2-1-测试代码"><a href="#2-1-测试代码" class="headerlink" title="2.1 测试代码"></a><font size=3>2.1 测试代码</font></h3><p>栈区的测试主要是通过局部变量的地址来看，如下所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">uint32_t</span> testValue = <span class="number">129</span>;</span><br><span class="line"><span class="type">uint8_t</span> testGroup[<span class="number">20</span>] = &#123;<span class="number">45</span>, <span class="number">65</span>, <span class="number">78</span>, <span class="number">54</span>, <span class="number">121</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    HAL_Init();</span><br><span class="line">    SystemClock_Config();</span><br><span class="line">    MX_GPIO_Init();</span><br><span class="line">    MX_USART1_UART_Init();</span><br><span class="line">    MX_FSMC_Init();</span><br><span class="line"></span><br><span class="line">    testValue ++;</span><br><span class="line">    testGroup[<span class="number">0</span>]++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testValue:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;testValue, testValue);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testGroup:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)testGroup, testGroup[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">    <span class="type">uint8_t</span> inerTestValue = <span class="number">19</span>;</span><br><span class="line">    inerTestValue++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;inerTestValue:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;inerTestValue, inerTestValue);</span><br><span class="line">	</span><br><span class="line">	<span class="type">uint8_t</span> *p;</span><br><span class="line">	p = (<span class="type">uint8_t</span> *)<span class="built_in">malloc</span>(<span class="number">20</span>);</span><br><span class="line">	*p = <span class="number">50</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;p:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)p, *p);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        LED0 = !LED0;</span><br><span class="line">        LED1 = !LED1;</span><br><span class="line">        HAL_Delay(<span class="number">500</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>完整工程可以查看<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/stm32f103-prj">STM32F103-Prj: STM32学习使用（STM32CubeMX+Makefile+VScode+J-Flash） (gitee.com)</a>。</p>
<h3 id="2-2-sct文件修改"><a href="#2-2-sct文件修改" class="headerlink" title="2.2 sct文件修改"></a><font size=3>2.2 sct文件修改</font></h3><p>我们修改sct文件如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">; *************************************************************</span><br><span class="line">; *** Scatter-Loading Description File generated by uVision ***</span><br><span class="line">; *************************************************************</span><br><span class="line"></span><br><span class="line">LR_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;    ; load region size_region</span><br><span class="line">  ER_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;  ; load address = execution address</span><br><span class="line">   *.o (RESET, +First)</span><br><span class="line">   *(InRoot$$Sections)</span><br><span class="line">   .ANY (+RO)</span><br><span class="line">   .ANY (+XO)</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line">   *.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-3-结果分析"><a href="#2-3-结果分析" class="headerlink" title="2.3 结果分析"></a><font size=3>2.3 结果分析</font></h3><p>我们的sct文件中执行域是这样的：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line"> *.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line"> .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">&#125;</span><br><span class="line">RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line"> .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里将栈区指定到RW_IRAM1，我们来看一下打印信息：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">testValue:<span class="number">0x20005000</span> <span class="number">130</span></span><br><span class="line">testGroup:<span class="number">0x20005004</span> <span class="number">46</span></span><br><span class="line">inerTestValue:<span class="number">0x200003F8</span> <span class="number">20</span></span><br><span class="line">p:<span class="number">0x20005188</span> <span class="number">50</span></span><br></pre></td></tr></table></figure>

<p>可以看到，局部变量被分配到了0x20000000起始的RW_IRAM1区域了。</p>
<h3 id="2-4-map文件分析"><a href="#2-4-map文件分析" class="headerlink" title="2.4 map文件分析"></a><font size=3>2.4 map文件分析</font></h3><p>我们来看一下map文件的Memory Map of the image：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530080034105.png" alt="image-20230530080034105" style="zoom:50%;" />

<h2 id="3-定义堆区到指定的内存区域"><a href="#3-定义堆区到指定的内存区域" class="headerlink" title="3. 定义堆区到指定的内存区域"></a><font size=3>3. 定义堆区到指定的内存区域</font></h2><p>前边我们已经看过了，当未指定HEAP的分配区域在哪个执行域的话，会优先使用最大的那个执行域。我们接下来看一下指定HEAP所在的执行域的情况。</p>
<h3 id="3-1-测试代码"><a href="#3-1-测试代码" class="headerlink" title="3.1 测试代码"></a><font size=3>3.1 测试代码</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">uint32_t</span> testValue = <span class="number">129</span>;</span><br><span class="line"><span class="type">uint8_t</span> testGroup[<span class="number">20</span>] = &#123;<span class="number">45</span>, <span class="number">65</span>, <span class="number">78</span>, <span class="number">54</span>, <span class="number">121</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    HAL_Init();</span><br><span class="line">    SystemClock_Config();</span><br><span class="line">    MX_GPIO_Init();</span><br><span class="line">    MX_USART1_UART_Init();</span><br><span class="line">    MX_FSMC_Init();</span><br><span class="line"></span><br><span class="line">    testValue ++;</span><br><span class="line">    testGroup[<span class="number">0</span>]++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testValue:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;testValue, testValue);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testGroup:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)testGroup, testGroup[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">    <span class="type">uint8_t</span> inerTestValue = <span class="number">19</span>;</span><br><span class="line">    inerTestValue++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;inerTestValue:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;inerTestValue, inerTestValue);</span><br><span class="line">	</span><br><span class="line">	<span class="type">uint8_t</span> *p;</span><br><span class="line">	p = (<span class="type">uint8_t</span> *)<span class="built_in">malloc</span>(<span class="number">20</span>);</span><br><span class="line">	*p = <span class="number">50</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;p:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)p, *p);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        LED0 = !LED0;</span><br><span class="line">        LED1 = !LED1;</span><br><span class="line">        HAL_Delay(<span class="number">500</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>完整工程可以查看<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/stm32f103-prj">STM32F103-Prj: STM32学习使用（STM32CubeMX+Makefile+VScode+J-Flash） (gitee.com)</a>。</p>
<h3 id="3-2-sct文件修改"><a href="#3-2-sct文件修改" class="headerlink" title="3.2 sct文件修改"></a><font size=3>3.2 sct文件修改</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">; *************************************************************</span><br><span class="line">; *** Scatter-Loading Description File generated by uVision ***</span><br><span class="line">; *************************************************************</span><br><span class="line"></span><br><span class="line">LR_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;    ; load region size_region</span><br><span class="line">  ER_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;  ; load address = execution address</span><br><span class="line">   *.o (RESET, +First)</span><br><span class="line">   *(InRoot$$Sections)</span><br><span class="line">   .ANY (+RO)</span><br><span class="line">   .ANY (+XO)</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line">   *.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line">   *.o(HEAP)                         ; 选择 HEAP 节区，堆</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-3-结果分析"><a href="#3-3-结果分析" class="headerlink" title="3.3 结果分析"></a><font size=3>3.3 结果分析</font></h3><p>我们的sct文件中执行域是这样的：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line"> *.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line"> *.o(HEAP)                         ; 选择 HEAP 节区，堆</span><br><span class="line"> .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">&#125;</span><br><span class="line">RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line"> .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里将栈区指定到RW_IRAM1，我们来看一下打印信息：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">testValue:<span class="number">0x20005000</span> <span class="number">130</span></span><br><span class="line">testGroup:<span class="number">0x20005004</span> <span class="number">46</span></span><br><span class="line">inerTestValue:<span class="number">0x200005F8</span> <span class="number">20</span></span><br><span class="line">p:<span class="number">0x20000018</span> <span class="number">50</span></span><br></pre></td></tr></table></figure>

<p>可以看到，malloc申请的内存空间被分配到了0x20000000起始的RW_IRAM1区域了。</p>
<h3 id="3-4-map文件分析"><a href="#3-4-map文件分析" class="headerlink" title="3.4 map文件分析"></a><font size=3>3.4 map文件分析</font></h3><p>我们再来看一下map文件：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530080717236.png" alt="image-20230530080717236" style="zoom:50%;" />

<h2 id="4-自定义一个节区"><a href="#4-自定义一个节区" class="headerlink" title="4. 自定义一个节区"></a><font size=3>4. 自定义一个节区</font></h2><p>__attribute__((section(“section_name”)))的方式可以将指定的变量定义到指定的内存区域去。这个时候我们需要在sct文件中创建一个这样的名为section_name的输入节区样式，需要注意的是当我们sct文件中没有这样一个输入节区的时候，他是会按照默认的情况选择最大的那个执行域的。</p>
<h3 id="4-1-测试代码"><a href="#4-1-测试代码" class="headerlink" title="4.1 测试代码"></a><font size=3>4.1 测试代码</font></h3><p>我们定义一个全局数组变量，并通过__attribute__((section(“section_name”)))的方式将其指定到对应的内存区域去。需要注意的是</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">uint32_t</span> testValue = <span class="number">129</span>;</span><br><span class="line"><span class="type">uint8_t</span> testGroup[<span class="number">20</span>] = &#123;<span class="number">45</span>, <span class="number">65</span>, <span class="number">78</span>, <span class="number">54</span>, <span class="number">121</span>&#125;;</span><br><span class="line"><span class="comment">//__attribute__((section(&quot;EXRAM&quot;)))</span></span><br><span class="line"><span class="type">uint8_t</span> EXtestGrup[<span class="number">1024</span>] __attribute__((section(<span class="string">&quot;my_section&quot;</span>))) =&#123;<span class="number">43</span>,<span class="number">32</span>,<span class="number">3</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    HAL_Init();</span><br><span class="line">    SystemClock_Config();</span><br><span class="line">    MX_GPIO_Init();</span><br><span class="line">    MX_USART1_UART_Init();</span><br><span class="line">    MX_FSMC_Init();</span><br><span class="line"></span><br><span class="line">    testValue ++;</span><br><span class="line">    testGroup[<span class="number">0</span>]++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testValue:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;testValue, testValue);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testGroup:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)testGroup, testGroup[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">    <span class="type">uint8_t</span> inerTestValue = <span class="number">19</span>;</span><br><span class="line">    inerTestValue++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;inerTestValue:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;inerTestValue, inerTestValue);</span><br><span class="line">	</span><br><span class="line">	<span class="type">uint8_t</span> *p;</span><br><span class="line">	p = (<span class="type">uint8_t</span> *)<span class="built_in">malloc</span>(<span class="number">20</span>);</span><br><span class="line">	*p = <span class="number">50</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;p:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)p, *p);</span><br><span class="line"></span><br><span class="line">	EXtestGrup[<span class="number">0</span>]++;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;EXtestGrup:0x%0X %d\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)EXtestGrup, EXtestGrup[<span class="number">0</span>]);</span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        LED0 = !LED0;</span><br><span class="line">        LED1 = !LED1;</span><br><span class="line">        HAL_Delay(<span class="number">500</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>完整工程可以查看<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/stm32f103-prj">STM32F103-Prj: STM32学习使用（STM32CubeMX+Makefile+VScode+J-Flash） (gitee.com)</a>。</p>
<h3 id="4-2-sct文件修改"><a href="#4-2-sct文件修改" class="headerlink" title="4.2 sct文件修改"></a><font size=3>4.2 sct文件修改</font></h3><h4 id="4-2-1-默认情况"><a href="#4-2-1-默认情况" class="headerlink" title="4.2.1 默认情况"></a><font size=3>4.2.1 默认情况</font></h4><p>也就是不在sct文件中指定自定义输入节区的位置：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530083036138.png" alt="image-20230530083036138" style="zoom:80%;" />

<p>最终的打印结果为：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">testValue:<span class="number">0x20005000</span> <span class="number">130</span></span><br><span class="line">testGroup:<span class="number">0x20005004</span> <span class="number">46</span></span><br><span class="line">inerTestValue:<span class="number">0x200005F8</span> <span class="number">20</span></span><br><span class="line">p:<span class="number">0x20000018</span> <span class="number">50</span></span><br><span class="line">EXtestGrup:<span class="number">0x20005034</span> <span class="number">44</span></span><br></pre></td></tr></table></figure>

<p>可以看到最后也是定义到了较大的执行域中。</p>
<h4 id="4-2-2-指定到较小的执行域"><a href="#4-2-2-指定到较小的执行域" class="headerlink" title="4.2.2 指定到较小的执行域"></a><font size=3>4.2.2 指定到较小的执行域</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">; *************************************************************</span><br><span class="line">; *** Scatter-Loading Description File generated by uVision ***</span><br><span class="line">; *************************************************************</span><br><span class="line"></span><br><span class="line">LR_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;    ; load region size_region</span><br><span class="line">  ER_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;  ; load address = execution address</span><br><span class="line">   *.o (RESET, +First)</span><br><span class="line">   *(InRoot$$Sections)</span><br><span class="line">   .ANY (+RO)</span><br><span class="line">   .ANY (+XO)</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line">   *.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line">   *.o(HEAP)                         ; 选择 HEAP 节区，堆</span><br><span class="line">   *.o(my_section)                   ; 选择 my_section 节区，自定义节区</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530084036075.png" alt="image-20230530084036075" style="zoom:80%;" />

<p>我们来看一下打印结果：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">testValue:<span class="number">0x20005000</span> <span class="number">130</span></span><br><span class="line">testGroup:<span class="number">0x20005004</span> <span class="number">46</span></span><br><span class="line">inerTestValue:<span class="number">0x200009F8</span> <span class="number">20</span></span><br><span class="line">p:<span class="number">0x20000418</span> <span class="number">50</span></span><br><span class="line">EXtestGrup:<span class="number">0x20000000</span> <span class="number">44</span></span><br></pre></td></tr></table></figure>

<p>可以看到，自定义节区生效了，我们的数组定义到了较小的RW_IRAM1中。</p>
<h1 id="三、选择性使用外部SRAM"><a href="#三、选择性使用外部SRAM" class="headerlink" title="三、选择性使用外部SRAM"></a><font size=3>三、选择性使用外部SRAM</font></h1><p>我们知道外部SRAM是通过FSMC来驱动的，外部SRAM的访问速度肯定是没有内部SRAM高的，但是我们内部SRAM空间不够的时候，是完全可以使用外部SRAM的，可以选择性的把一些特别大的变量分配到外部。但是默认的情况还是分配到内部SRAM。</p>
<p>在后边我们学习SRAM的时候，需要读写 SRAM 存储的内容时，需要使用指针或者 __attribute__((at(具体地址)))  来指定变量的位置，当有多个这样的变量时，就需要手动计算地址空间了，非常麻烦在这一节中我们将修改 sct 文件，让链接器自动分配全局变量到外部 SRAM的地址并进行管理，这将会使得利用外部 SRAM 的空间就跟内部 SRAM 一样简单。 </p>
<h2 id="1-SRAM初始化"><a href="#1-SRAM初始化" class="headerlink" title="1. SRAM初始化"></a><font size=3>1. SRAM初始化</font></h2><p>在使用外部SRAM之前肯定是初始化外部SRAM啦，按照后边的笔记完成初始化即可。但是可能会有一些问题，我们来分析一下，就是我们知道程序运行的时候，会把ROM中的RW类型的数据拷贝到SRAM中去。</p>
<p>所以我想要把变量定义到外部SRAM去，比如我们定义了一个全局的数据testGroup[1024]，它就会被复制到SRAM中，若是指定它分配在外部的SRAM中的话，它将会被复制到外部的SRAM中。这个时候需要保证，在RW拷贝数据之前就将SRAM初始化好，不然我们都要进行数据拷贝了，但是还没有进行SRAM的初始化，这样我们的数据就会发生错误。所以我们需要在执行数据拷贝之前，就完成外部SRAM的初始化。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/MDK006.jpeg" alt="应用程序的加载视图与执行视图" style="zoom:50%;" />

<p>复制过程发生在哪里？其实前边我们有了解过，我们通过以下命令获取一个反汇编文件（还是使用野火的流水灯工程：<a target="_blank" rel="noopener" href="https://gitee.com/Embedfire-stm32f103-badao/ebf_stm32f103_badao_std_code">ebf_stm32f103_badao_std_code: 野火STM32F103 霸道开发板 标准库教程配套代码 (gitee.com)</a>或者我在本地也保存了一个：<a href="./STM32%E5%BC%80%E5%8F%91%E7%9B%B8%E5%85%B3%E8%B5%84%E6%96%99/03MDK/%E7%A8%8B%E5%BA%8F/MDK%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3-GPIO%E8%BE%93%E5%87%BA%E2%80%94%E5%A4%9A%E5%BD%A9%E6%B5%81%E6%B0%B4%E7%81%AF/Project/RVMDK%EF%BC%88uv5%EF%BC%89/BH-F103.uvprojx">流水灯例程</a>）：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">PS D:\MyLinux\Ubuntu\Sharedfiles\3Linux\16-LV16\ebf_stm32f103_badao_std_code-master\46-MDK编译过程及文件全解\程序\MDK文</span><br><span class="line">件详解-GPIO输出—多彩流水灯\Output&gt; fromelf --text -c .\流水灯.axf --output ../elfInfo/流水灯_axf_elfInfo_c.txt</span><br></pre></td></tr></table></figure>

<p>然后我们打开反汇编文件“流水灯_axf_elfInfo_c.txt”的反汇编信息，可以看到到程序中具有一段名为“__scatterload”的分散加载代码，它是由 armlink 链接器自动生成的，里边会有很多LDM指令，这些就是用于复制数据的：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 分散加载代码(流水灯_axf_elfInfo_c.txt文件)</span></span><br><span class="line">    .text</span><br><span class="line">    __scatterload</span><br><span class="line">    __scatterload_rt2</span><br><span class="line">        <span class="number">0x08000168</span>:    <span class="number">4</span>c06        .L      LDR      r4,[pc,#<span class="number">24</span>] ; [<span class="number">0x8000184</span>] = <span class="number">0x80005c4</span></span><br><span class="line">        <span class="number">0x0800016a</span>:    <span class="number">4</span>d07        .M      LDR      r5,[pc,#<span class="number">28</span>] ; [<span class="number">0x8000188</span>] = <span class="number">0x80005d4</span></span><br><span class="line">        <span class="number">0x0800016c</span>:    e006        ..      B        <span class="number">0x800017c</span> ; __scatterload + <span class="number">20</span></span><br><span class="line">        <span class="number">0x0800016e</span>:    <span class="number">68e0</span>        .h      LDR      r0,[r4,#<span class="number">0xc</span>]</span><br></pre></td></tr></table></figure>

<p>而LDM指令的操作数中包含了加载的源地址，这些地址中包含了内部FLASH存储的RW-data数据，执行这些指令后数据就会从FLASH地址加载到内部SRAM的地址。我们再来看一下这个文件的__main的反汇编代码部分：</p>
<figure class="highlight txt"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">//__main的反汇编代码部分（流水灯_axf_elfInfo_c.txt文件）</span><br><span class="line">    .ARM.Collect$$$$00000000</span><br><span class="line">    .ARM.Collect$$$$00000001</span><br><span class="line">    __Vectors_End</span><br><span class="line">    __main</span><br><span class="line">    _main_stk</span><br><span class="line">        0x08000130:    f8dfd00c    ....    LDR      sp,__lit__00000000 ; [0x8000140] = 0x20000400</span><br><span class="line">    .ARM.Collect$$$$00000004</span><br><span class="line">    _main_scatterload</span><br><span class="line">        0x08000134:    f000f818    ....    BL       __scatterload ; 0x8000168</span><br></pre></td></tr></table></figure>

<p>可以看最后一行， “__scatterload ”的代码会被“__main”函数调用，__main在启动文件中的“Reset_Handler”会被调用，因而，在主体程序执行前，已经完成了分散加载过程。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530093755738.png" alt="image-20230530093755738" style="zoom:50%;" />

<p>接下来就是修改我们的启动文件了，由于我使用的是STM32CubeMX生成的工程，在FSMC初始化之前就还需要一些时钟啊，之类的初始化，所以我干脆把这些必要的初始化全部移到启动文件中，他们执行完毕后再去main主函数。其实从这里就可以看出，main主函数无非也就是一个函数而已，我们完全可以在进入main之前做一些其他的事情：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530094653845.png" alt="image-20230530094653845" style="zoom: 33%;" />

<p>总的来说，芯片启动后，会通过 __main 函数调用分散加载代码 __scatterload，分散加载代码会把存储在 FLASH 中的 RW-data 复制到 RAM 中，然后在 RAM区开辟一块 ZI-data 的空间，并将其初始化为 0 值。所以在外部 SRAM 正常运转之前，分散加载过程复制到外部SRAM 中的数据都丢失了，因而在初始化外部 SRAM 之后，需要重新给变量赋值才能正常使用(即定义变量时的初值无效，在调用 FSMC_SRAM_Init 函数之后的赋值才有效)。  因此，为了保证在程序中定义到外部 SRAM中的变量能被正常初始化，我们需要在系统执行分散加载代码之前使外部 SRAM 存储器正常运转，使它能够正常保存数据。  </p>
<p>在原来的启动文件中增加FSMC初始化的代码（注意要包括完整的初始化过程），增加的代码中使用到汇编语法的 IMPOR引入在 sram.c 文件中定义的 MX_FSMC_Init函数，接着使用 LDR 指令加载函数的代码地址到寄存器 R0，最后使用 BLX R0 指令跳转到 MX_FSMC_Init的代码地址执行。以上代码实现了 Reset_handler 在执行 __main 函数前先调用了我们自定义的 MX_FSMC_Init函数，从而为分散加载代码准备好正常的硬件工作环境。</p>
<h2 id="2-定义一个新的执行域"><a href="#2-定义一个新的执行域" class="headerlink" title="2. 定义一个新的执行域"></a><font size=3>2. 定义一个新的执行域</font></h2><h3 id="2-1-不指定栈区"><a href="#2-1-不指定栈区" class="headerlink" title="2.1 不指定栈区"></a><font size=3>2.1 不指定栈区</font></h3><p>我们先来看这样会不会有问题：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">; *************************************************************</span><br><span class="line">; *** Scatter-Loading Description File generated by uVision ***</span><br><span class="line">; *************************************************************</span><br><span class="line"></span><br><span class="line">LR_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;    ; load region size_region</span><br><span class="line">  ER_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;  ; load address = execution address</span><br><span class="line">   *.o (RESET, +First)</span><br><span class="line">   *(InRoot$$Sections)</span><br><span class="line">   .ANY (+RO)</span><br><span class="line">   .ANY (+XO)</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line">   ;*.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line">   *.o(HEAP)                         ; 选择 HEAP 节区，堆</span><br><span class="line">   *.o(my_section)                   ; 选择 my_section 节区，自定义节区</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM3 <span class="number">0x68000000</span> <span class="number">0x00100000</span> &#123;   ; RW data 外部 SRAM 区域 <span class="number">1</span>MB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们新增一个RW_IRAM3，地址就是我们外部SRAM的起始地址0x68000000，我们不指定栈区的位置，我们看一下map文件：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530095728076.png" alt="image-20230530095728076" style="zoom:80%;" />

<p>会发现，栈区自动被分配到了最大的执行域RW_IRAM3，而栈区里边都是什么?我们定义的局部变量啊什么的都是存在于栈区的，栈区属于ZI-data类型，它是要放在SRAM中才能正常工作。这样问题就来了，我们在启动文件中做初始化，就拿FSMC初始化为例，我们看一下里边的内容：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530095921199.png" alt="image-20230530095921199" style="zoom:50%;" />

<p>这不也都是大量的局部变量嘛？但是这个时候外部SRAM还没准备好啊，这样栈区就是不可用的，这些初始化直接就挂掉了，所以这样程序根本无法运行。这里就是一个”死循环”：FSMC初始化的时候要使用栈空间，但是栈空间又只能等FSMC初始化完外部SRAM才能分配到外部SRAM，这样就大家都在等，那这样肯定是跑不动的。</p>
<h3 id="2-2-指定栈区"><a href="#2-2-指定栈区" class="headerlink" title="2.2 指定栈区"></a><font size=3>2.2 指定栈区</font></h3><p>那上边的问题怎么解决？我们知道内部的SRAM是直接就可用的，我们直接将栈区分配到内部SRAM就可以了啊，没必要把栈空间也放到外部SRAM的，所以我们可以这样修改scrt文件：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">; *************************************************************</span><br><span class="line">; *** Scatter-Loading Description File generated by uVision ***</span><br><span class="line">; *************************************************************</span><br><span class="line"></span><br><span class="line">LR_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;    ; load region size_region</span><br><span class="line">  ER_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;  ; load address = execution address</span><br><span class="line">   *.o (RESET, +First)</span><br><span class="line">   *(InRoot$$Sections)</span><br><span class="line">   .ANY (+RO)</span><br><span class="line">   .ANY (+XO)</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line">   *.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line">   *.o(HEAP)                         ; 选择 HEAP 节区，堆</span><br><span class="line">   *.o(my_section1)                  ; 选择 my_section1 节区，自定义节区</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM3 <span class="number">0x68000000</span> <span class="number">0x00100000</span> &#123;   ; RW data 外部 SRAM 区域 <span class="number">1</span>MB</span><br><span class="line">   *.o(my_section2)                  ; 选择 my_section2 节区，自定义节区</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样我们将栈空间和自定义的节区my_section1都定义到内部SRAM，然后定义一个新的节区my_section2放到外部SRAM中去。但是这样写还是会有问题的，这里依然跑不动程序，这一掉部分只修改栈区位置，下一部分笔记再解决另一个问题。</p>
<p>【注意】其实我们还有一个办法，那就是直接操作寄存器，因为我们直接去动寄存器的话，就不会有局部变量这些啦。</p>
<h3 id="2-3-只指定特定的变量到外部SRAM"><a href="#2-3-只指定特定的变量到外部SRAM" class="headerlink" title="2.3 只指定特定的变量到外部SRAM"></a><font size=3>2.3 只指定特定的变量到外部SRAM</font></h3><p>我们看一下上一个指定栈区后的遗留问题：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530101824606.png" alt="image-20230530101824606" style="zoom:50%;" />

<p>我们看一下这种写法有什么问题，我们在RW_IRAM3中写了.ANY(+RW +ZI)，这就意味着我们的RW-data和ZI-data将会也优先分配到这里，这两种类型里边的都是一些全局变量，我们在调用__main之前还调用了那么多函数，特别是在STM32CubeMX中，它在初始化时钟和串口以及FSMC的时候都在用全局变量，这样的话，不就和栈区一样，陷入死循环了吗？我们其实只是有需要的时候将变量定义到外部SRAM，所以我们完全可以只分配使用我们自定义的节区的变量到外部SRAM区，我们可以修改sct文件如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">; *************************************************************</span><br><span class="line">; *** Scatter-Loading Description File generated by uVision ***</span><br><span class="line">; *************************************************************</span><br><span class="line"></span><br><span class="line">LR_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;    ; load region size_region</span><br><span class="line">  ER_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;  ; load address = execution address</span><br><span class="line">   *.o (RESET, +First)</span><br><span class="line">   *(InRoot$$Sections)</span><br><span class="line">   .ANY (+RO)</span><br><span class="line">   .ANY (+XO)</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line">   *.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line">   *.o(HEAP)                         ; 选择 HEAP 节区，堆</span><br><span class="line">   *.o(my_section1)                  ; 选择 my_section1 节区，自定义节区</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM3 <span class="number">0x68000000</span> <span class="number">0x00100000</span> &#123;   ; RW data 外部 SRAM 区域 <span class="number">1</span>MB</span><br><span class="line">   *.o(my_section2)                  ; 选择 my_section2 节区，自定义节区</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后我们再看一下map文件：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530102348151.png" alt="image-20230530102348151" style="zoom:50%;" />

<p>这样我们就只有使用了自定义节区的变量才会定义到外部SRAM中。</p>
<h3 id="2-4-堆空间"><a href="#2-4-堆空间" class="headerlink" title="2.4 堆空间"></a><font size=3>2.4 堆空间</font></h3><p>我们知道堆空间是通过malloc来分配的，而在我们外部SRAM初始化完成之前的哪些部分中并没有用到堆空间，所以我们完全可以把堆空间直接定义到外部SRAM中去：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">; *************************************************************</span><br><span class="line">; *** Scatter-Loading Description File generated by uVision ***</span><br><span class="line">; *************************************************************</span><br><span class="line"></span><br><span class="line">LR_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;    ; load region size_region</span><br><span class="line">  ER_IROM1 <span class="number">0x08000000</span> <span class="number">0x00080000</span>  &#123;  ; load address = execution address</span><br><span class="line">   *.o (RESET, +First)</span><br><span class="line">   *(InRoot$$Sections)</span><br><span class="line">   .ANY (+RO)</span><br><span class="line">   .ANY (+XO)</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line">   *.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line">   *.o(my_section1)                  ; 选择 my_section1 节区，自定义节区</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line">   .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">  &#125;</span><br><span class="line">  RW_IRAM3 <span class="number">0x68000000</span> <span class="number">0x00100000</span> &#123;   ; RW data 外部 SRAM 区域 <span class="number">1</span>MB</span><br><span class="line">   *.o(my_section2)                  ; 选择 my_section2 节区，自定义节区</span><br><span class="line">   *.o(HEAP)                         ; 选择 HEAP 节区，堆</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="3-测试代码"><a href="#3-测试代码" class="headerlink" title="3. 测试代码"></a><font size=3>3. 测试代码</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="type">uint32_t</span> testValue = <span class="number">129</span>;</span><br><span class="line"><span class="type">uint8_t</span> testGroup[<span class="number">20</span>] = &#123;<span class="number">45</span>, <span class="number">65</span>, <span class="number">78</span>, <span class="number">54</span>, <span class="number">121</span>&#125;;</span><br><span class="line"><span class="comment">//__attribute__((section(&quot;EXRAM&quot;)))</span></span><br><span class="line"><span class="type">uint8_t</span> EXtestGrup1[<span class="number">1024</span>] __attribute__((section(<span class="string">&quot;my_section1&quot;</span>))) =&#123;<span class="number">43</span>,<span class="number">32</span>,<span class="number">3</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">uint8_t</span> EXtestGrup2[<span class="number">1024</span>] __attribute__((section(<span class="string">&quot;my_section2&quot;</span>))) =&#123;<span class="number">43</span>,<span class="number">32</span>,<span class="number">3</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    testValue ++;</span><br><span class="line">    testGroup[<span class="number">0</span>]++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testValue:0x%0X %d\r\n\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;testValue, testValue);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;testGroup:0x%0X %d\r\n\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)testGroup, testGroup[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">    <span class="type">uint8_t</span> inerTestValue = <span class="number">19</span>;</span><br><span class="line">    inerTestValue++;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;inerTestValue:0x%0X %d\r\n\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)&amp;inerTestValue, inerTestValue);</span><br><span class="line">	</span><br><span class="line">	<span class="type">uint8_t</span> *p;</span><br><span class="line">	p = (<span class="type">uint8_t</span> *)<span class="built_in">malloc</span>(<span class="number">20</span>);</span><br><span class="line">	*p = <span class="number">50</span>;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;p:0x%0X %d\r\n\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)p, *p);</span><br><span class="line"></span><br><span class="line">	EXtestGrup1[<span class="number">0</span>]++;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;EXtestGrup1:0x%0X %d\r\n\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)EXtestGrup1, EXtestGrup1[<span class="number">0</span>]);</span><br><span class="line">	EXtestGrup2[<span class="number">0</span>]++;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;EXtestGrup2:0x%0X %d\r\n\r\n&quot;</span>, (<span class="type">unsigned</span> <span class="type">int</span>)EXtestGrup2, EXtestGrup2[<span class="number">0</span>]);</span><br><span class="line">	</span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        LED0 = !LED0;</span><br><span class="line">        LED1 = !LED1;</span><br><span class="line">        HAL_Delay(<span class="number">500</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-结果分析"><a href="#4-结果分析" class="headerlink" title="4. 结果分析"></a><font size=3>4. 结果分析</font></h2><p>我们看一下打印的结果：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-06-MDK-03-SCT%E6%96%87%E4%BB%B6%E5%BA%94%E7%94%A8/img/image-20230530103740629.png" alt="image-20230530103740629" style="zoom:50%;" />

<p>此时我们的sct文件的执行域是这样的：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">RW_IRAM1 <span class="number">0x20000000</span> <span class="number">0x00005000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">1</span> <span class="number">20</span>KB</span><br><span class="line"> *.o(STACK)                        ; 选择 STACK 节区，栈</span><br><span class="line"> *.o(my_section1)                  ; 选择 my_section1 节区，自定义节区</span><br><span class="line"> .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">&#125;</span><br><span class="line">RW_IRAM2 <span class="number">0x20005000</span> <span class="number">0x00007000</span>  &#123;  ; RW data 内部 SRAM 区域<span class="number">2</span> <span class="number">28</span>KB</span><br><span class="line"> .ANY (+RW +ZI)                    ; 所有的 RW/ZI-data 都分配到这里</span><br><span class="line">&#125;</span><br><span class="line">RW_IRAM3 <span class="number">0x68000000</span> <span class="number">0x00100000</span> &#123;   ; RW data 外部 SRAM 区域 <span class="number">1</span>MB</span><br><span class="line"> *.o(my_section2)                  ; 选择 my_section2 节区，自定义节区</span><br><span class="line"> *.o(HEAP)                         ; 选择 HEAP 节区，堆</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/4329ee27.html">LV16-06-MDK-03-SCT文件应用</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年06月04日 - 22:06</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/4329ee27.html" title="LV16-06-MDK-03-SCT文件应用">https://sumumm.github.io/post/4329ee27.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV16-STM32%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV16-STM32开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/daff314e.html" rel="prev" title="LV16-07-开发工具-01-STM32CubeMX-Makefile">
                  <i class="fa fa-angle-left"></i> LV16-07-开发工具-01-STM32CubeMX-Makefile
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/e1abea0c.html" rel="next" title="LV16-06-MDK-02-工程文件类型">
                  LV16-06-MDK-02-工程文件类型 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
